package com.alifi.crm.web.home.screen;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.buc.sso.client.util.SimpleUserUtil;
import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.common.lang.StringUtil;
import com.alibaba.common.logging.Logger;
import com.alibaba.common.logging.LoggerFactory;
import com.alibaba.platform.buc.sso.common.dto.SimpleSSOUser;
import com.alifi.crm.biz.guess.Base;
import com.alifi.crm.biz.guess.GuessMessage;
import com.alifi.crm.biz.score.EmployeeScoreManager;
import com.alifi.crm.dal.enu.States;
import com.alifi.crm.dal.object.EmployeeScoreDO;

/**
 * 
 * @author wenjing.huangwj
 *
 */
public class score {

	@Resource(name="employeeScoreManager")
	EmployeeScoreManager employeeScoreManager;
	
	protected static final Logger      logger = LoggerFactory.getLogger(score.class);
	
	protected static final Logger      logPermission = LoggerFactory.getLogger("uums-permission-logger");
	
	
	public void execute(HttpServletRequest request, HttpServletResponse response,Navigator nav,final Context ctx){
		try {
			if(StringUtil.equalsIgnoreCase((String)ctx.get("action"), "true")){   //如果经过action，直接返回
				return;
			}
			SimpleSSOUser user = SimpleUserUtil.findUser(request);   // 姓名: lastName   花名: nickNameCn 
			if((user==null)||StringUtil.isBlank(user.getEmpId())){   //检查
				ctx.put("message", GuessMessage.NOT_GET_LOGIN_INFO);
				return;
			}
			if (logPermission.isInfoEnabled()) {
				logPermission.info(user.getEmpId()+"  come in  score.htm.");
			}
			ctx.put("user", user);
			
		    EmployeeScoreDO scoreDO = employeeScoreManager.getEmployeeScore(Base.toSixBitSmployee(user.getEmpId()));
			if(scoreDO==null){
				ctx.put("message", GuessMessage.NOT_FOUND_EMPLOYEE);
				return;
			}
			
		    if(States.准备.equals(scoreDO.getScoreStates())||scoreDO.getScoreStates()==null){
		    	employeeScoreManager.comeIn(scoreDO.getEmployeeNum());
		    }
		    
		    if(States.结束.equals(scoreDO.getScoreStates())){
		    	ctx.put("message", GuessMessage.SCORE_END);
		    	return;
		    }else{
		    	ctx.put("continue_score", true);
		    }
		   
		    ctx.put("guessDO", scoreDO);
		} catch (Exception e) {
			ctx.put("message", e.getMessage());
			logger.error(e);  
		} 
	}

}
